using System;
using System.Windows;
using System.Windows.Controls;
namespace Coding4Fun.Phone.Controls
{
	public class DynamicTextBlock : ContentControl
	{
		public const string TooLongFlagStr = "...";
		private const string string_0 = "DynamicTextBlock";
		public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(DynamicTextBlock), new PropertyMetadata(null, new PropertyChangedCallback(DynamicTextBlock.smethod_0)));
		public static readonly DependencyProperty TextWrappingProperty = DependencyProperty.Register("TextWrapping", typeof(TextWrapping), typeof(DynamicTextBlock), new PropertyMetadata(1, new PropertyChangedCallback(DynamicTextBlock.smethod_1)));
		public static readonly DependencyProperty LineHeightProperty = DependencyProperty.Register("LineHeight", typeof(double), typeof(DynamicTextBlock), new PropertyMetadata(0.0, new PropertyChangedCallback(DynamicTextBlock.smethod_2)));
		public static readonly DependencyProperty LineStackingStrategyProperty = DependencyProperty.Register("LineStackingStrategy", typeof(LineStackingStrategy), typeof(DynamicTextBlock), new PropertyMetadata(1, new PropertyChangedCallback(DynamicTextBlock.smethod_3)));
		private TextBlock textBlock_0;
		public bool IsTooLong;
		public string Text
		{
			get
			{
				return (string)base.GetValue(DynamicTextBlock.TextProperty);
			}
			set
			{
				base.SetValue(DynamicTextBlock.TextProperty, value);
			}
		}
		public TextWrapping TextWrapping
		{
			get
			{
				return (TextWrapping)base.GetValue(DynamicTextBlock.TextWrappingProperty);
			}
			set
			{
				base.SetValue(DynamicTextBlock.TextWrappingProperty, value);
			}
		}
		public double LineHeight
		{
			get
			{
				return (double)base.GetValue(DynamicTextBlock.LineHeightProperty);
			}
			set
			{
				base.SetValue(DynamicTextBlock.LineHeightProperty, value);
			}
		}
		public LineStackingStrategy LineStackingStrategy
		{
			get
			{
				return (LineStackingStrategy)base.GetValue(DynamicTextBlock.LineStackingStrategyProperty);
			}
			set
			{
				base.SetValue(DynamicTextBlock.LineStackingStrategyProperty, value);
			}
		}
		private static void smethod_0(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			((DynamicTextBlock)dependencyObject_0).OnTextChanged(dependencyPropertyChangedEventArgs_0);
		}
		protected virtual void OnTextChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			base.InvalidateMeasure();
		}
		private static void smethod_1(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			((DynamicTextBlock)dependencyObject_0).OnTextWrappingChanged(dependencyPropertyChangedEventArgs_0);
		}
		protected virtual void OnTextWrappingChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			this.textBlock_0.set_TextWrapping((TextWrapping)dependencyPropertyChangedEventArgs_0.get_NewValue());
			base.InvalidateMeasure();
		}
		private static void smethod_2(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			((DynamicTextBlock)dependencyObject_0).OnLineHeightChanged(dependencyPropertyChangedEventArgs_0);
		}
		protected virtual void OnLineHeightChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			this.textBlock_0.set_LineHeight(this.LineHeight);
			base.InvalidateMeasure();
		}
		private static void smethod_3(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			((DynamicTextBlock)dependencyObject_0).OnLineStackingStrategyChanged(dependencyPropertyChangedEventArgs_0);
		}
		protected virtual void OnLineStackingStrategyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			this.textBlock_0.set_LineStackingStrategy((LineStackingStrategy)dependencyPropertyChangedEventArgs_0.get_NewValue());
			base.InvalidateMeasure();
		}
		public DynamicTextBlock()
		{
			this.textBlock_0 = new TextBlock();
			base.set_Content(this.textBlock_0);
		}
		protected override Size MeasureOverride(Size availableSize)
		{
			this.IsTooLong = false;
			bool flag = this.TextWrapping == 2;
			if (!string.IsNullOrEmpty(this.Text) && this.Text.get_Length() > 2)
			{
				Size size_ = flag ? new Size(availableSize.get_Width(), double.PositiveInfinity) : new Size(double.PositiveInfinity, availableSize.get_Height());
				int num = this.method_0(this.Text, size_, availableSize);
				if (num == this.Text.get_Length() - 1)
				{
					this.textBlock_0.set_Text(this.Text.Substring(0, num + 1));
				}
				else
				{
					this.IsTooLong = true;
					this.textBlock_0.set_Text(this.Text.Substring(0, num + 1) + "...");
				}
				return base.MeasureOverride(availableSize);
			}
			this.textBlock_0.set_Text(this.Text);
			return base.MeasureOverride(availableSize);
		}
		private int method_0(string string_1, Size size_0, Size size_1)
		{
			int i = 0;
			int num = string_1.get_Length() - 1;
			if (this.method_1(string_1, num, size_0, size_1) < 0)
			{
				return num;
			}
			while (i <= num)
			{
				if (i != num)
				{
					if (num != i + 1)
					{
						int num2 = (i + num) / 2;
						if (this.method_1(string_1, num2, size_0, size_1) > 0)
						{
							num = num2;
							continue;
						}
						i = num2;
						continue;
					}
				}
				return i;
			}
			return -1;
		}
		private int method_1(string string_1, int int_0, Size size_0, Size size_1)
		{
			bool flag = this.TextWrapping == 2;
			if (int_0 == string_1.get_Length() - 1)
			{
				this.textBlock_0.set_Text(string_1.Substring(0, int_0 + 1));
			}
			else
			{
				this.IsTooLong = true;
				this.textBlock_0.set_Text(string_1.Substring(0, int_0 + 1) + "...");
			}
			Size size = base.MeasureOverride(size_0);
			if (flag)
			{
				if (size.get_Height() <= size_1.get_Height())
				{
					return -1;
				}
				return 1;
			}
			else
			{
				if (size.get_Width() <= size_1.get_Width())
				{
					return -1;
				}
				return 1;
			}
		}
		protected virtual string ReduceText(string text)
		{
			return text.Substring(0, text.get_Length() - 1);
		}
	}
}
